#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>

#define BUF_SIZE 48

int main(int argc, char* argv[]) {

  char buf[BUF_SIZE]; 
  if(argc != 3){
    fprintf(stderr, "Please use correctilly 'yapc :-)' fileSource fileDestination  \n", *argv);
    exit(1);
  }

  int fileSource;
  fileSource = open(argv[1],O_RDONLY);
  /*this will be triggered in case there is no Source file*/
  if (fileSource < 0) {
    perror("Error no source file");
    exit(1);
  } 

  int fileDestination;
  fileDestination = open(argv[2], O_WRONLY | O_EXCL | O_CREAT, 0644);
  /*if it's a directory or the file already exists this will be triggered*/
  if (fileDestination == -1) {
    perror("Error fileDestination"); 
    exit(1);
    }

  int fileSize;
  fileSize = read(fileSource,buf,BUF_SIZE);
  while (fileSize > 0) {
    int mistake = write(fileDestination,buf,fileSize);
    if(mistake == -1) {
    perror("Bad Descriptor");
    exit(1);
    }
    fileSize = read(fileSource,buf,BUF_SIZE);
    if (fileSize == -1){ 
    perror("Error"); 
    exit(1); 
      }
    }
  return 0;
} 
